package com.cloudspace.yunloan.player.base;

import android.content.Context;

import com.cloudspace.yunloan.player.exo.ExoPlayerSystem;
import com.cloudspace.yunloan.player.interfaces.IMediaPlayer;

public abstract class AbsMediaPlayer implements IMediaPlayer {

    public final static int TYPE_SYSTEM = 0;
    public final static int TYPE_EXO = 1;

    public static final int STATE_ERROR = -1;
    public static final int STATE_IDLE = 0;
    public static final int STATE_PREPARING = 1;
    public static final int STATE_PREPARED = 2;
    public static final int STATE_PLAYING = 3;
    public static final int STATE_PAUSED = 4;
    public static final int STATE_PLAYBACK_COMPLETED = 5;
    public static final int STATE_SEEKING = 6;
    public static final int STATE_ERROR_RETRY = 7;


    protected int mPlayState = STATE_IDLE;

    public static AbsMediaPlayer getMediaPlayer(Context context, int type) {
        switch (type) {
            case AbsMediaPlayer.TYPE_SYSTEM:
                return new MediaPlayerSystem(context);
            case AbsMediaPlayer.TYPE_EXO:
                return new ExoPlayerSystem(context);
            default:
                return new ExoPlayerSystem(context);
        }
    }

    public int getPlayState() {
        return mPlayState;
    }

    public void setPlayState(int state) {
        mPlayState = state;
    }

    // For media player callback
    protected OnPreparedListener mOnPreparedListener;

    protected OnCompletionListener mOnCompletionListener;

    protected OnErrorListener mOnErrorListener;

    protected OnBufferingUpdateListener mOnBufferingUpdateListener;

    protected OnPlayStateChangeListener mOnPlayStateChangeListener;// Defined

    protected OnPlaySwitchListener mOnPlaySwitchListener;// Defined

    protected OnVideoSizeChangedListener mVideoSizeChangedListener;

    public void setOnPreparedListener(OnPreparedListener mOnPreparedListener) {
        this.mOnPreparedListener = mOnPreparedListener;
    }

    public void setOnCompletionListener(OnCompletionListener mOnCompletionListener) {
        this.mOnCompletionListener = mOnCompletionListener;
    }

    public void setOnErrorListener(OnErrorListener mOnErrorListener) {
        this.mOnErrorListener = mOnErrorListener;
    }

    public void setOnBufferingUpdateListener(OnBufferingUpdateListener mBufferingUpdateListener) {
        this.mOnBufferingUpdateListener = mBufferingUpdateListener;
    }

    public void setOnPlayStateChangeListener(OnPlayStateChangeListener mPlayStateChangeListener) {
        this.mOnPlayStateChangeListener = mPlayStateChangeListener;
    }

    public void setOnPlaySwitchListener(OnPlaySwitchListener mPlaySwitchListener) {
        this.mOnPlaySwitchListener = mPlaySwitchListener;
    }

    public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener mVideoSizeChangedListener) {
        this.mVideoSizeChangedListener = mVideoSizeChangedListener;
    }

    public interface OnPreparedListener {
        void onPrepared(Object mp);
    }

    public interface OnCompletionListener {
        void onCompletion(Object mp);
    }

    public interface OnErrorListener {
        boolean onError(Object mp, int what, int extra);
    }

    public interface OnBufferingUpdateListener {
        void onBufferingUpdate(Object mp, int percent);
    }

    public interface OnPlayStateChangeListener {
        void onPlayStateChange(int playState);
    }

    public interface OnPlaySwitchListener {
        void onPlaySwitch(int direction, boolean isForcedMusic);
    }

    public interface OnVideoSizeChangedListener {
        void onVideoSizeChanged(Object mp, int arg1, int arg2);
    }

}
